package com.yl.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.entity.*;
import com.yl.auth.model.entity.Chuangwei;
import com.yl.auth.model.entity.JiaShu;
import com.yl.auth.model.entity.Laoren;
import com.yl.auth.model.entity.RuyuanfeiBean;
import com.yl.auth.model.vo.ChuYuanQueryVo;
import com.yl.auth.model.vo.RuYuanQueryVo;
import com.yl.auth.model.vo.YudingQueryVo;
import com.yl.auth.service.*;
import com.yl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yl
 * @since 2023-06-25
 */
@RestController
    @RequestMapping("/admin/auth/yudingdengji")
@Api(tags = "市场销售-预定登记管理")
public class YudingdengjiController {

    @Resource
    private YudingdengjiService yudingdengjiService;

    @Resource
    private LaorenService laorenService;

    @Resource
    private JiaShuService jiaShuService;

    @Resource
    private HulidengjiService hulidengjiService;

    @Resource
    private RuyuanfeiService ruyuanfeiService;

    @Resource
    private ChuangweiService chuangweiService;

    @Resource
    private FangjianService fangjianService;

    @Resource
    private FangjianleixingService fangjianleixingService;


    @GetMapping("/findAllConnAndPage/{page}/{limit}")
    @ApiOperation(value = "预定登记查询分页条件查询")
    public Result findAllConnAndPage(@PathVariable Integer page, @PathVariable Integer limit, YudingQueryVo yudingQueryVo) {
        Page<Yudingdengji> pageParam = new Page<>(page,limit);
        Page<Yudingdengji> pages =  yudingdengjiService.selectPage(pageParam,yudingQueryVo);
        //获取page中的所有数据
        List<Yudingdengji> records = pages.getRecords();
        for (Yudingdengji record : records) {
            Integer bedId = record.getBedId();
            Chuangwei chuangwei = chuangweiService.getById(bedId);
            Fangjian fangjian = fangjianService.getById(chuangwei.getRoomId());
            String[] split = fangjian.getRoomNo().toString().split(""); //对房间号进行分割
            //声明一个变量接受数据中第一个值
            String str = "";
            for (int i = 0; i <split.length; i++) {
                str = split[0];  //吧第一个值赋值给str
            }
            //将拼接的数据放进属性中
            record.setChuangWeiYu(str+"楼"+fangjian.getRoomNo()+"室"+chuangwei.getBedNo()+"床");
        }
        return Result.ok(pages);
    }

    @PostMapping("/save/{id}")
    @ApiOperation(value = "新增预定登记")
    public Result save (@RequestBody Yudingdengji yudingdengji,@PathVariable Long id) {
        yudingdengji.setDengji(LocalDate.now());
        yudingdengji.setLaorenId(id);
        //根据预定登记老人的id获取老人的指定的数据
        Laoren laoren = laorenService.getById(yudingdengji.getLaorenId());
        //修改状态
        laoren.setStatus(1);
        //将对象进行更新
        laorenService.updateById(laoren);
        //添加预定登记信息
        yudingdengjiService.save(yudingdengji);
        return Result.ok();
    }

    @PostMapping("/addYvDingXiaDan/{id}")
    @ApiOperation(value = "新增预约订单但是不提交")
    public Result addYvDingXiaDan (@RequestBody Yudingdengji yudingdengji,@PathVariable Long id) {
        yudingdengji.setDengji(LocalDate.now());
        yudingdengji.setLaorenId(id);

        //根据预定登记老人的id获取老人的指定的数据
        Laoren laoren = laorenService.getById(yudingdengji.getLaorenId());
        //修改状态
        laoren.setStatus(1);
        //将对象进行更新
        laorenService.updateById(laoren);

        //添加预定登记信息
        //把预订登记表中的状态改为 预定中
        yudingdengji.setStatus(1);
        yudingdengjiService.save(yudingdengji);

        return Result.ok();
    }


    @PutMapping("/updateYuDing")
    @ApiOperation(value = "修改预定登记")
    public Result updateYuDing(@RequestBody Yudingdengji yudingdengji) {
        yudingdengjiService.updateById(yudingdengji);
        return Result.ok();
    }

    @GetMapping("/get/{id}")
    @ApiOperation(value = "根据指定的id回显预定登记数据")
    public Result get(@PathVariable Integer id) {
        Yudingdengji yudingdengji = yudingdengjiService.getById(id);
        return Result.ok(yudingdengji);
    }


    @DeleteMapping("/deleteById/{id}")
    @ApiOperation(value = "删除指定的预定数据信息")
    public Result deleteById(@PathVariable Integer id) {
        //根据预定id获取到老人的外键
        Yudingdengji yudingdengji = yudingdengjiService.getById(id);
        //根据预定登记老人的id获取老人的指定的数据
        Laoren laoren = laorenService.getById(yudingdengji.getLaorenId());
        //修改状态
        laoren.setStatus(0);
        //将对象进行更新
        laorenService.updateById(laoren);
        yudingdengjiService.removeById(id);
        return Result.ok();
    }

    @GetMapping("/push/{id}")
    @ApiOperation(value = "提交预定功能")
    public Result push(@PathVariable Integer id) {
        yudingdengjiService.publish(id);
        return Result.ok();
    }

    @GetMapping("/quxiao/{id}")
    @ApiOperation(value = "取消预定功能")
    public Result quxiao(@PathVariable Integer id){
        yudingdengjiService.quxiao(id);
        return Result.ok();
    }

    @GetMapping("/getLaorenYuding/{id}")
    @ApiOperation(value = "根据id获取指定的预定信息和老人信息")
    public Result getLaorenYuding(@PathVariable Integer id) {
        Yudingdengji yudingdengji = yudingdengjiService.getLaorenYuding(id);
        return Result.ok(yudingdengji);
    }

    @GetMapping("/findAllRuYuan/{page}/{limit}")
    @ApiOperation(value = "入院管理分页查询条件")
    public Result findAllRuYuan(@PathVariable Integer page, @PathVariable Integer limit,
                                RuYuanQueryVo ruYuanQueryVo){
        Page<Yudingdengji> pagePram = new Page<>(page,limit);
        Page<Yudingdengji> pages = yudingdengjiService.getfindAllRuYuan(pagePram,ruYuanQueryVo);
        //对床位进行处理
        List<Yudingdengji> records = pages.getRecords();
        for (Yudingdengji record : records) {
            System.out.println("--------------------=============="+record);
            Chuangwei chuangwei = chuangweiService.getById(record.getBedId());
            System.out.println("--------------------=============="+chuangwei);
            Fangjian fangjian = fangjianService.getById(chuangwei.getRoomId());
            record.setRuyuanChuangWei(fangjian.getRoomNo()+"/0"+chuangwei.getBedNo());
        }
        return Result.ok(pages);
    }

    @GetMapping("/findXinRuYuan")
    @ApiOperation(value = "新增入院申请展示功能")
    public Result findXinRuYuan() {
        List<Yudingdengji> yudingdengjiList = yudingdengjiService.findXinRuYuan();
        return Result.ok(yudingdengjiList);
    }

    @GetMapping("/updateStaus/{id}")
    @ApiOperation(value = "根据id指定数据的状态")
    public Result getStatus(@PathVariable Integer id) {
        Yudingdengji yudingdengji = yudingdengjiService.getById(id);
        yudingdengji.setStatus(3); //将状态修改为入院待提交的状态
        yudingdengji.setShenqing(LocalDate.now()); //自动生成申请的时候
        yudingdengjiService.updateById(yudingdengji);
        return Result.ok(yudingdengji);
    }

    @GetMapping("/ruYuanXiangQing/{id}")
    @ApiOperation(value = "入院详情功能")
    public Result ruYuanXiangQing(@PathVariable Integer id) {
        //根据id获取指定的预定人信息
        Yudingdengji yudingdengji = yudingdengjiService.getById(id);
        //通过预定人的信息查询到指定的老人的信息
        Laoren laoren = laorenService.getById(yudingdengji.getLaorenId());
        //然后在通过老人的信息查询到指定的护理等级的数据信息
        HulidengjiBean hulidengjiBean = hulidengjiService.getById(laoren.getCareLevelId());
        //在老人表中声明一个stirng类型的变量将护理等级的名字赋值 给那个声明的变量
        laoren.setHuliNameRuyuan(hulidengjiBean.getDengji());
        //使用mybatis-puls的条件构造器进行对家属的老人外键和老人的id进行判断
        LambdaQueryWrapper<JiaShu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JiaShu::getLaorenId,yudingdengji.getLaorenId());
        //随后将家属list出来将我们刚才使用的条件构造器判断的数据放进list当中
        List<JiaShu> list = jiaShuService.list(wrapper);
        //使用mybatis-puls的条件构造器进行对入院费用管理的老人外键和老人的id进行判断
        LambdaQueryWrapper<RuyuanfeiBean> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RuyuanfeiBean::getLaorenId,yudingdengji.getLaorenId());
        //随后将入院核定费用getone出来因为只有一个是一对一的关系，再把判断出来的数据放进getone中
        RuyuanfeiBean ruyuanfeiBean = ruyuanfeiService.getOne(queryWrapper);
        //声明一个map集合，因为是多个对象还有集合，将所有的数据存放进map当中到了前端可以随时往map当中去拿很方便
        Map<String,Object> map = new HashMap<>();
        map.put("laoren",laoren);
        map.put("jiaList",list);
        map.put("ruyuanfeiBean",ruyuanfeiBean);
        map.put("yudingdengji",yudingdengji);
        return Result.ok(map);
    }

    @GetMapping("/tijiaoruyuan/{id}")
    @ApiOperation(value = "提交入院按钮功能")
    public Result tijiaoruyuan(@PathVariable Integer id) {
        Yudingdengji byId = yudingdengjiService.getById(id);
        byId.setStatus(4);
        yudingdengjiService.updateById(byId);
        return Result.ok(byId);
    }

    @GetMapping("/deleteRuYuan/{id}")
    @ApiOperation(value = "入院删除按钮功能")
    public Result deleteRuYuan(@PathVariable Integer id) {
        Yudingdengji yudingdengji = yudingdengjiService.getById(id);
        yudingdengji.setStatus(1);
        yudingdengjiService.updateById(yudingdengji);
        return Result.ok(yudingdengji);
    }
    @GetMapping("/quXiaoRuYuan/{id}")
    @ApiOperation(value = "取消入院按钮功能")
    public Result quxiaoruyuan(@PathVariable Integer id) {
        Yudingdengji yudingdengji = yudingdengjiService.getById(id);
        yudingdengji.setStatus(7);
        yudingdengjiService.updateById(yudingdengji);
        return Result.ok(yudingdengji);
    }

    @GetMapping("/xuanzefuwu/{id}")
    @ApiOperation(value = "选择服务老人信息和家属信息展示")
    public Result xuanzefuwu(@PathVariable Integer id) {
        Yudingdengji yudingdengji = yudingdengjiService.getById(id);
        Laoren laoren = laorenService.getById(yudingdengji.getLaorenId());
        List<HulidengjiBean> huList = hulidengjiService.list();
        for (HulidengjiBean hulidengjiBean : huList) {
            Long hid = hulidengjiBean.getId();
            if(laoren.getCareLevelId()==hid) {
                laoren.setHuliname(hulidengjiBean.getDengji());
            }
        }
        LambdaQueryWrapper<JiaShu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JiaShu::getLaorenId,laoren.getId());
        List<JiaShu> list = jiaShuService.list(wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("laoren",laoren);
        map.put("list",list);
        return Result.ok(map);
    }

    @GetMapping("/xiadantijiao/{id}")
    @ApiOperation(value = "订单确定提交并保存修改状态为已完成状态")
    public Result xiadantijiao(@PathVariable Integer id) {
        Yudingdengji yudingdengji = yudingdengjiService.getById(id);
        Laoren laoren = laorenService.getById(yudingdengji.getLaorenId());
        //修改老人的状态
        laoren.setStatus(2);
        //添加床位的信息
        //根据入院核定管理指定床位，在床位赋值给老人表中
        LambdaQueryWrapper<RuyuanfeiBean> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RuyuanfeiBean::getLaorenId,laoren.getId());
        RuyuanfeiBean ruyuanfeiBean = ruyuanfeiService.getOne(queryWrapper);
        laoren.setBedId(ruyuanfeiBean.getBedId());
        laorenService.updateById(laoren);
        //修改床位的状态
        Chuangwei chuangwei = chuangweiService.getById(ruyuanfeiBean.getBedId());
        chuangwei.setStatus(2);
        chuangweiService.updateById(chuangwei);
        LambdaQueryWrapper<Yudingdengji> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Yudingdengji::getLaorenId,laoren.getId());
        //查询所有的预定信息
        Yudingdengji yudingdengjis = yudingdengjiService.getOne(wrapper);
        yudingdengjis.setStatus(6);
        yudingdengjiService.updateById(yudingdengjis);
        return Result.ok();
    }

    @GetMapping("/chuYuanFindAllPage/{page}/{limit}")
    @ApiOperation(value = "出院管理列表分页查询条件模糊查询")
    public Result chuYuanFindAllPage(@PathVariable Integer page,@PathVariable Integer limit,
                                     ChuYuanQueryVo chuYuanQueryVo){
        Page<Yudingdengji> pageParm = new Page<>(page,limit);
        Page<Yudingdengji> pages = yudingdengjiService.ChuYuanPage(pageParm,chuYuanQueryVo);
        return Result.ok(pages);
    }

    @GetMapping("/xZChuQing")
    @ApiOperation(value = "新增出院请选择展示功能")
    public Result xZChuQing(){
        List<Yudingdengji> yuList = yudingdengjiService.xZChuQing();
        return Result.ok(yuList);
    }


    @PostMapping("/xinZengChuYuan/{id}")
    @ApiOperation(value = "新增出院申请功能")
    public Result xinZengChuYuan(@RequestBody Yudingdengji yudingdengji ,@PathVariable Integer id) {
        Yudingdengji yuding = yudingdengjiService.getById(id);
        yuding.setChuyuandate(yudingdengji.getChuyuandate());
        yuding.setChuyuandesc(yudingdengji.getChuyuandesc());
        yuding.setStatus(8); //修改新增出院的状态为待出院的状态
        yudingdengjiService.updateById(yuding);
        return Result.ok();
    }

    @GetMapping("/xiuGaiChuYuan/{id}")
    @ApiOperation(value = "回显出院信息")
    public Result xiuGaiChuYuan(@PathVariable @ApiParam("预定id") Integer id){
        Yudingdengji yudingdengji = yudingdengjiService.getById(id);
        return Result.ok(yudingdengji);
    }

    @PutMapping("/bianJiChu")
    @ApiOperation(value = "编辑出院信息")
    public Result bianJiChu(@RequestBody @ApiParam("预定实体类") Yudingdengji yudingdengji){
        yudingdengjiService.updateById(yudingdengji);
        return Result.ok();
    }

    @GetMapping("/queDingChuYuan/{id}")
    @ApiOperation(value = "确定出院按钮功能")
    public Result queDingChuYuan(@PathVariable Integer id){
        Yudingdengji r = yudingdengjiService.getById(id);
        Laoren laoren = laorenService.getById(r.getLaorenId());
        laoren.setStatus(3);
        r.setStatus(9);
        laorenService.updateById(laoren);
        yudingdengjiService.updateById(r);
        return Result.ok(r);
    }

    @GetMapping("/huixianLaorenChuangWei/{id}/{tianshu}")
    @ApiOperation(value = "回显床位指定的价格")
    public Result huixianLaorenChuangWei(@PathVariable Integer id,@PathVariable(required = false) double tianshu){
        //根据前端返回的床位id调用床位对象
        Chuangwei chuangwei = chuangweiService.getById(id);
        //根据床位的房间外键查询房间
        Fangjian fangjian = fangjianService.getById(chuangwei.getRoomId());
        //在根据房间的类型外键查询房间类型列表
        Fangjianleixing fangjianleixing = fangjianleixingService.getById(fangjian.getRoomType());
        chuangwei.setXiaPrice(fangjianleixing.getPrice());
        chuangwei.setXiaTotal(chuangwei.getXiaPrice() * tianshu);
        System.out.println("------------------------------------------------------"+chuangwei.getXiaPrice());
        chuangweiService.updateById(chuangwei);
        return Result.ok(chuangwei);
    }

    @DeleteMapping("/deleteByChuYuan/{id}")
    @ApiOperation(value = "出院删除功能")
    public Result deleteByChuYuan(@PathVariable @ApiParam("预定id") Integer id) {
        Yudingdengji byId = yudingdengjiService.getById(id);
        Laoren laoren = laorenService.getById(byId.getLaorenId());
        laorenService.removeById(laoren.getId());
        yudingdengjiService.removeById(id);
        QueryWrapper<Chuangwei> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("laoren_id",byId.getLaorenId());
        Chuangwei byId1 = chuangweiService.getOne(queryWrapper);
        byId1.setLaorenId((long) 0);
        byId1.setStatus(0);
        chuangweiService.updateById(byId1);
        return Result.ok();
    }

    @GetMapping("/deleteByChuYuanStatus/{id}")
    @ApiOperation(value = "出院删除功能 修改状态")
    public Result deleteByChuYuanStatus(@PathVariable @ApiParam("预定id") Integer id){
        Yudingdengji byId = yudingdengjiService.getById(id);
        byId.setStatus(6);
        boolean b = yudingdengjiService.updateById(byId);
        if (b){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

}

